<?php if (!defined('THINK_PATH')) exit();?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src='/Public/jquery.min.js'></script>
    <script src='/Public/foundation/js/foundation.min.js'></script>
    <link rel="stylesheet" href="/Public/foundation/css/foundation.min.css">
</head>
<body>
<div class="small-2 columns">
<ul class="side-nav">
 <?php if(is_array($nav)): $i = 0; $__LIST__ = $nav;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vo): $mod = ($i % 2 );++$i;?><li><a href="<?php echo U($vo.url);?>" target='<?php echo ($vo["target"]?"_blank":null); ?>'><?php echo ($vo["mname"]); ?></a></li><?php endforeach; endif; else: echo "" ;endif; ?>
    </ul>
</div>
<div class="small-10 columns">
<table id='Tbl'>
<thead>
    <tr>
        <th><input type="checkbox" onclick="selectAll(this)" value='全选'></th>
        <th>编号</th>
        <th>菜单名称</th>
        <th>链接地址</th>
        <th>打开方式</th>
        <th>上级菜单</th>
        <th>分组</th>
        <th>排序</th>
        <th>锁定</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <?php if(is_array($menus)): $i = 0; $__LIST__ = $menus;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vo): $mod = ($i % 2 );++$i;?><tr id="row_<?php echo ($vo["menu_id"]); ?>">
            <td><input type="checkbox" class='select' value='<?php echo ($vo["menu_id"]); ?>' ></td>
            <td><?php echo ($vo["menu_id"]); ?></td>
            <td><input name="mname" class="clss" disabled type="text" value="<?php echo ($vo["mname"]); ?>"></td>
            <td><input name="url" class="clss" disabled type="text" value="<?php echo ($vo["url"]); ?>"></td>
            <td>
                    <div class="switch tiny">
                        <input onclick="target(this)" value="<?php echo ($vo["menu_id"]); ?>" id="target_<?php echo ($vo["menu_id"]); ?>" type="checkbox"  <?php echo ($vo['target']?'checked':''); ?>>
                        <label for="target_<?php echo ($vo["menu_id"]); ?>"></label>
                    </div>
                </td>
            <td>
                    <select class="clss" disabled name="up" id="">
                        <option <?php echo ($vo['up']==0?'selected':null); ?>value="0">顶级</option>
                        <?php if(is_array($opts)): $i = 0; $__LIST__ = $opts;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$opt): $mod = ($i % 2 );++$i;?><option <?php echo ($vo['up']==$opt['menu_id']?'selected':null); ?> value="<?php echo ($opt['menu_id']); ?>"><?php echo ($opt["mname"]); ?></option><?php endforeach; endif; else: echo "" ;endif; ?>
                    </select>
                </td>
            <td><input name="mgroup" class="clss" disabled style="width:4em;" type="text" value="<?php echo ($vo["mgroup"]); ?>"></td>
            <td><input name="msort" class="clss" disabled style="width:4em;" type="text" value="<?php echo ($vo["msort"]); ?>"></td>
                
            <td>
            
             <div class="switch tiny">
              <input onclick="lock(this)" value="<?php echo ($vo["menu_id"]); ?>" id="lock_<?php echo ($vo["menu_id"]); ?>" type="checkbox"  <?php echo ($vo['lock']?'checked':''); ?>>
                <label for="lock_<?php echo ($vo["menu_id"]); ?>"></label>
                </div>
               
            </td> 
            <td>
            <button type="button" onclick='edit("<?php echo ($vo["menu_id"]); ?>")' class='edit'>编辑</button>
            <button style='display:none' onclick='save("<?php echo ($vo["menu_id"]); ?>")' type="button" class="save">保存</button>
            </td>
        </tr><?php endforeach; endif; else: echo "" ;endif; ?>
    </tbody>
</table>
<input type="button" class="button" onclick="moreDel()" value='删除(批量)'>
<input type="button" class="button" onclick="unSelect()" value='反选'>
<script>
        function selectAll(obj) {
            $('#Tbl tbody .select').prop('checked',$(obj).prop('checked'));
        }
        function unSelect() {
            $('#Tbl tbody .select').map(function(){
                $(this).prop('checked',!$(this).prop('checked'));
            });
        }
        function moreDel() {
            if(!confirm('确定删除吗?')){
                return;
            }
            var ids = $('#Tbl tbody .select:checked').map(function () {
                $(this).parent().parent().remove();
                return $(this).val();
            });
            ids  = ids.get().join();
            $.post('<?php echo U("doDelMenu");?>',{ids:ids},function (data) {
                if(data.error ==0)alert(data.msg);
            });
        }
        function lock(obj) {
            var id = $(obj).val();
            var lockval = $(obj).prop('checked')?1:0;
            $.post('<?php echo U("doEditMenu");?>',{menu_id:id,lock:lockval});
        }
        function target(obj) {
            var id = $(obj).val();
            var targetval = $(obj).prop('checked')?1:0;
            $.post('<?php echo U("doEditMenu");?>',{menu_id:id,target:targetval},function (data) {
            console.log(data);
                        });
        }
        
        function edit(id) {
        	console.log(id);
            $('#row_'+id+' .clss').prop('disabled',false);
            $('#row_'+id+' .edit').hide();
            $('#row_'+id+' .save').show();
        }

        function save(id) {
            $('#row_'+id+' .clss').prop('disabled',true);
            $('#row_'+id+' .edit').show();
            $('#row_'+id+' .save').hide();

            var data = {
                menu_id:id,
                mname:$('#row_'+id+' [name=mname]').val(),
                url:  $('#row_'+id+' [name=url]').val(),
                mgroup :  $('#row_'+id+' [name=mgroup]').val(),
                msort :  $('#row_'+id+' [name=msort]').val(),
                up : $('#row_'+id+' [name=up]').val(),
            }

            $.post('<?php echo U("doEditMenu");?>',data,function (data) {
                if(data.error!==0){
                    alert(data.msg);
                }
            });

        }
        </script>
</div>

</body>
</html>